<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="${ctx }/css/base.css" rel="stylesheet">
<link rel="stylesheet" href="${ctx }/custom/uimaker/easyui.css">
<link href="${ctx }/css/process.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="${ctx }/custom/uimaker/icon.css">

</head>
<body>
	<div id="cc" class="easyui-layout" style="width:100%;height:650px;">   
	    <div data-options="region:'center',title:'所有试卷'" style="padding:0px;background:#eee;">
	    	<table id="dg"
     		 data-options="
     		 	fit:true,
     		 	rownumbers:true,
     		 	pagination:true,
     		 	striped:true,
     		 	singleSelect:true,
     		 	fitColumns:true,
     		 	toolbar:'#tb',
     		 	url:'${ctx }/test/examFind',
     		 	nowrap:false
     		 "
     		>
     			<thead>
     				<tr>
     					<th field="ck" data-options="checkbox:true"></th>
     					<th field="title" width="250">题目</th>
     					<th field="loginName" width="80">出卷人</th>
     					<th field="lesson" width="60">科目</th>
     					<th field="score" width="60">分数</th>
     					<th field="strDate" width="250">题目组成</th>
     					<th field="type" width="70"
     					data-options="
     					 formatter:function(v,r,i){
     					 	if('yes'==v){
     					 		v = '是';
     					 	}else if('no'==v){
     					 		v = '否';
     					 	}
     					 	return v;
     					 }
     					"
     					>已使用?</th>
     					<th field="year" width="80">所属年份</th>
     					<th field="createDate" align="center" width="150" data-options="
     						formatter:function(v,r,i){
     						    //2018-04-03 00:00:00
     							if(v.length>=11){
     							   v = v.substring(0,11);
     							}
     							return v;
     						}
     					">出卷时间</th>
     					<th field="action" align="center" width="100" data-options="
     						formatter:function(v,r,i){
     						     var str = '';
     						    if(r.type!='yes'){ 
	     						    str = '<a href=\'#\''
	     						    +' onclick=\'editExam('+r.id+')\'>修改</a></br>';
	     						    
	     						    str += '<hr/><a href=\'#\' onclick=\'createExam('+i+')\'>生成试卷</a>';
     						    }else{
     						    	str += '<a href=\'#\' onclick=\'checkExam('+i+')\'>查看试卷</a>';
     						    }
     							return str;
     						}
     					">操作</th>
     				</tr>
     			</thead>
     		</table>
     		<div id="tb" style="padding:0 30px;">
     			<c:if test="${eu.type eq '0' }">
	        	科目: <input class="easyui-textbox" type="text" name="lesson" style="width:166px;height:35px;line-height:35px;"></input>
	        	</c:if>
	        	所属年份: <input class="easyui-textbox" type="text" name="year" style="width:166px;height:35px;line-height:35px;"></input>
	        <a href="#" class="easyui-linkbutton" iconCls="icon-search" data-options="selected:true">查询</a>
	        <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="addExam()">新增</a>
	      </div>
	    </div>   
	</div> 
	<script type="text/javascript" src="${ctx }/custom/jquery.min.js"></script>
    <script type="text/javascript" src="${ctx }/custom/jquery.easyui.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="${ctx }/js/jquery-form.js"></script>
    <script type="text/javascript" src="${ctx }/js/extEasyUI.js"></script>
    <script type="text/javascript" src="${ctx }/js/extJquery.js"></script>
	<script type="text/javascript">
	    var res=0; 
	    $(function(){
	    	$('#dig1').window('close');  
	    	$('#dig2').window('close');  
	    	$('#dg').datagrid({
	    		
	    	});
	    	
	    });
		function addExam(){
			openDialog('新增试卷');
		}
		function editExam(){
			openDialog('编辑试卷');
		}
		function openDialog(title){
			$('body').append('<div id="d1"></div>');
			$('#d1').dialog({
				title:title,
				modal:true,
				width:800,
				height:500,
				href:'${ctx}/test/examAddOrEdit',
				buttons:[{
					text:'保存',
					iconCls:'icon-save',
					handler:function(){
						var f = $('#dataForm');
						if(!f.form('validate')){
							return;
						}
						
						var ps = $('#titleTypeTd p');	
						var str = '';
						var flat = false;
						$.each(ps,function(i,p){
							var se = $(this).find("select");
							var id = $(se).attr('id');
							var val = $('#'+id).combobox('getValue');
							
							var type = val;
							
							var input = $(this).find('.easyui-numberspinner');
							val = $(input).val();
							type += "-"+val;
							if(flat){
								str += ';';
							}
							flat = true;
							str += type;
						});
						//sign-30;muti-10
						$('#titleType_').val(str);
						$.ajax({
							type:'post',
							url:'${ctx}/test/examAdd',
							data:f.serialize(),
							dataType:'json',
							success:function(r){
								alert(r.msg);
								if(r.sus){
									$('#d1').dialog('destroy');
									$('#dg').datagrid('reload');
								}
							}
						});
					}
				},{
					text:'取消',
					iconCls:'icon-cancel',
					handler:function(){
						$('#d1').dialog('destroy');
					}
				}]
			});
		}
		
		function createExam(i){
			$('#dg').datagrid('selectRow',i);
			var row = $('#dg').datagrid('getSelected');
			//alert(row.score+"--"+row.ts);
			$('#dig1').window('open'); 
			$('#title01').empty().html(row.title);
			var tts = row.ts;
			var str = '';
			$.each(tts,function(i,tt){
				var tty = tt.titleType;
				if('sign'==tt.titleType){
					tty = '单选题';
				}else if('muti'==tt.titleType){
					tty = '多选题';
				}else if('is'==tt.titleType){
					tty = '判断题';
				}else if('text'==tt.titleType){
					tty = '主观题';
				}else if('input'==tt.titleType){
					tty = '填空题';
				}
				str += '<p lon="'+row.lesson+'" examId="'+row.id+'" tt="'+tt.titleType+'" tty="'+tty+'" ts="'+tt.scoreCount+'" style="font-size:16px;margin: 8px">'+
				    	''+(i+1)+'、'+tty+' - '+tt.scoreCount+'分，'+
				    	'题目数：'+
				    	'<input class="easyui-textbox" style="width:100px;height: 30px">'+
				    	'</p>';
			});
			$('#titleTypes').empty().html(str);
			/* $('#titleTypes').empty();
			$('#titleTypes').html(str); */
		}
		
		function randomCreate(){
			var ps = $('#titleTypes p');
			var str = '[',flat=false;
			$.each(ps,function(i,p){
				var eid = $(this).attr("examId");
				var tt = $(this).attr("tt");
				var tty = $(this).attr("tty");
				var ts = $(this).attr("ts");
				var lesson = $(this).attr('lon');
				var inp = $(this).find('input');
				var tnum = inp.val();
				//str = "[{},{},{}]"
				var json = "{'examId':"+eid+",'titleType':'"+tt+"','name':'"+tty+"','scoreCount':"+ts+",'strDate':'"+lesson+"','cons':"+tnum+"}";
				if(flat){
					str += ",";
				}
				flat = true;
				str += json;
			});
			str += ']';
			$.ajax({
				type:'post',
				url:'${ctx}/test/randomCreate',
				data:{
					code:str
				},
				dataType:'json',
				success:function(rss){
					$('#dig2').window('open'); 
					$('#dig1').window('close'); 
					$('#title02').html($('#title01').html());
					var str = '';
					var check = 'checked';
					res = rss;
					$.each(rss,function(i,rs){
						if(i>0){
							check='';
						}
						str += '<div id="checkFAS" '+
						    'style="border: 1px solid #000;margin: 8px;width: 320px;float: left;">';
				    		$.each(rs,function(j,r){
				    			str +='<p style="font-size:16px;margin: 8px">'+
					    		    	''+(j+1)+'、'+r.name+'(每题<font style="color:red">'+r.code+'</font>分) - '+r.scoreCount+'分，'+
					    		    	'题目数：'+r.cons+''+
					    		    	'</p>';
				    		});
						    str+='<p style="text-align: center;margin: 8px 0px;">'+
					    		  '<input '+check+' type="radio" id="faId'+i+'" name="fa">方案'+(i+1)+''+
						    	 '</p>';
						    
				        str +='</div>';
					});
					$('#fas').empty().html(str);
				}
			});
		}
		
		function confirmCreate(){
			var fas = $('#checkFAS');
			var str = '';
			for(var i=0;i<fas.length;i++){
				var fa = fas[i];
				var ra = $(fa).find('input[type="radio"]');
				var id = ra.attr('id');
				if((document.getElementById(id)).checked){
					var re = res[i];
					//把json对象转化为json字符串
					str = JSON.stringify(re);
					break;
				}
			}
			$.ajax({
				sync:false,
				type:'post',
				url:'${ctx}/test/gotoCreate',
				data:{
					code:str
				},
				dataType:'json',
				success:function(r){
					alert(r.msg);
					if(r.sus){
						
					}
				}
			});
		}
		
		function checkExam(index){
			//window.open('${ctx}/test/examCheck');
			
			$('#dg').datagrid('selectRow',index);
			var row = $('#dg').datagrid('getSelected');
			 $('#dig3').dialog({
				title:'查看试卷',
				fit:true,
				modal:true,
				href:'${ctx}/test/examCheck?examId='+row.id
			}); 
		}
		
		
		
		
		
		
		
		
		
    </script>
    <div id="dig3"></div>
    <div id="dig1" class="easyui-dialog" 
	    title="选择试卷生成的方式" 
	    style="width:400px;height:300px;"   
	        data-options="iconCls:'icon-save',
	        resizable:true,modal:true">   
	    <center>
	    	<h2 id="title01"
	    	style="font-size:24px;margin: 10px">试卷标题</h2>
	    </center>
	    <hr/>
	    <div id="titleTypes">
	    	
	    	
	    </div>
	    <center>
	    <a class="easyui-linkbutton" onclick="randomCreate()">自动生成</a>
	    <a class="easyui-linkbutton">手动生成</a>
	    </center>
	</div>  
	
	 <div id="dig2" class="easyui-dialog" 
	    title="选择试卷生成的方案" 
	    style="width:680px;height:400px;"   
	        data-options="iconCls:'icon-save',
	        resizable:true,modal:true,
	     	buttons:[{
	     		text:'确定生成',
	     		iconCls:'icon-save',
	     		handler:function(){
	     			confirmCreate();
	     		}
	     	}]
	     ">   
	    <center>
	    	<h2 id="title02"
	    	style="font-size:24px;margin: 10px">试卷标题</h2>
	    </center>
	    <hr/>
	    <span id="fas">
	    
	    </span>
	</div>  

	
	
	
	
	
	
	
	
	
	
	
	
	
	
</body>
</html>